package com.mingdao.lhy.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mingdao.lhy.biz.WishFillingBiz;
import com.mingdao.lhy.pojo.HighSchool;
import com.mingdao.lhy.pojo.HighSchoolWish;
import com.mingdao.lhy.pojo.SignUpStudent;
import com.mingdao.lhy.pojo.WishAll;
import com.mingdao.lhy.pojo.WishOne;
import com.mingdao.lhy.pojo.WishThree;
import com.mingdao.lhy.pojo.WishTwo;
import com.mingdao.lhy.service.WishFillingService;

@Controller
@RequestMapping(value = "wishOneTwoThreeSignUp")
public class WishOneTwoThreeController {
	private static Logger log = LoggerFactory.getLogger(WishOneTwoThreeController.class);
	
	/**
	 * #高中志愿填报起止时间
	 */
	@Value("${gzzytbkssj}")
	private String gzzytbkssj;
	
	@Value("${gzzytbjssj}")
	private String gzzytbjssj;

	@Autowired
	private WishFillingService wishFillingService;

	@Autowired
	private WishFillingBiz wishFillingBiz;

	@RequestMapping("")
	public String index(Model model, @RequestParam("bmh") String bmh) {

		HttpSession session = wishFillingBiz.getSession();
		if (null == session || null == session.getAttribute("student")) {
			session = null;
			log.error("用户未登录，请重新登录");
			return "redirect:/";
		}

		if (null == bmh || bmh.isEmpty()) {
			model.addAttribute("msg", "没接收到学生信息");
			return "redirect:/";
		}
		/**
		 * 根据传入学生信息先判断是否已经报名，已经报名就显示已报名的信息供修改和确认
		 * 
		 */
		HighSchoolWish highSchoolWish = wishFillingService.getOneTwoThreeStudentByBmh(bmh);
		if (null == highSchoolWish) {
			log.warn("指定报名号的学生还没有进行第一、二、三批次志愿填报");
		} else {
			// xm bmh sfzh byzxdm bjdm wbxh
		}
		model.addAttribute("highSchoolWish", highSchoolWish);

		/**
		 * 根据学生报名表获取学生对应的第1，第2，第3批次可填报学校名单
		 */
		SignUpStudent student = wishFillingService.getStudentByBmh(bmh);
		model.addAttribute("student", student);

		/**
		 * 报名时间判断 6月25日08:00--6月27日18:00之间可用 判断是否是州外考生，如果是则返回引导页面
		 */
		Date day = new Date();

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date now = df.parse(df.format(day)); // 当前时间
			Date startTime = df.parse(gzzytbkssj);
			Date endTime = df.parse(gzzytbjssj); // 截止时间
			if (now.before(endTime) && now.after(startTime)) {
				// 继续正常处理
				System.out.println("继续正常处理");

			} else {
				model.addAttribute("message", "高中志愿非填报时间，不能报名");
				return "/transform";
			}
		} catch (Exception exception) {
			exception.printStackTrace();
			return "/transform";
		}
		if (null != student.getValid() && "0".equals(student.getValid()) || "3".equals(student.getKslb())) {

			model.addAttribute("message", "非凉山州户籍或毕业一年以上，不能填报高中志愿");
			return "/transform";
		}

		/**
		 * 取第一学校
		 */
		WishOne wishOne = wishFillingService.getWishOneSchool(student);

		// Boolean isOneClass = wishFillingBiz.isOneClassOfSystems(student);

		WishTwo wishTwo = new WishTwo();
		WishThree wishThree = new WishThree();

		/**
		 * 一类体制志愿： 前提是一类体制学生和普通学生的彝文加试成绩大于等于30的学生，都要根据考生户籍所在县市分县市单独判断。
		 * 一类志愿学样选择，从gzdm.ylmsjh大于0的学校。
		 */
		List<HighSchool> oneClassSchool = new ArrayList<HighSchool>();
		String[] xxdms = wishFillingBiz.getOneClassSchool(student);
		// StringBuffer sb = new StringBuffer();
		List<String> xxdm = new ArrayList<String>();
		for (int i = 0; i < xxdms.length; i++) {
			xxdm.add(xxdms[i]);
		}
		// String xxdm = sb.toString();
		// xxdm = xxdm.substring(0, xxdm.length() - 1);
		oneClassSchool = wishFillingService.getOneClassOfSystems(xxdm);
		wishOne.setOneClassOfSystems(oneClassSchool);
		/* if (!isOneClass) { */
		/**
		 * 取第二批次学校
		 */
		wishTwo = wishFillingService.getWishTwoSchool(student);
		/**
		 * 取第三批次学校
		 */
		wishThree = wishFillingService.getWishThreeSchool(student);

		/* } */

		WishAll wishAll = null;
		if (null == session.getAttribute("wishAll")) {
			wishAll = new WishAll();
		} else {
			wishAll = (WishAll) session.getAttribute("wishAll");
		}

		wishAll.setWishOne(wishOne);
		wishAll.setWishTwo(wishTwo);
		wishAll.setWishThree(wishThree);
		model.addAttribute("wishAll", wishAll);
		session.setAttribute("wishAll", wishAll);

		return "wishFillingStudent";

	}

}
